package com.havilahshawn.finaltest22ndsesforad;


import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.havilahshawn.finaltest22ndsesforad.adapter.StuAdapter;
import com.havilahshawn.finaltest22ndsesforad.dao.Student;

import java.util.ArrayList;
import java.util.List;

import static com.havilahshawn.finaltest22ndsesforad.DB.StuContract.StuEntry.CONTENT_URI;

public class MainActivity extends AppCompatActivity {

    private List<Student> students;
    private StuAdapter stuAdapter;
    private ListView stuListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        students = new ArrayList<>();
        stuListView = findViewById(R.id.student_list);
        View emptyView = findViewById(R.id.empty_view);
        stuListView.setEmptyView(emptyView);



        stuAdapter = new StuAdapter(getApplicationContext(),students);
        stuListView.setAdapter(stuAdapter);
        Uri uri = Uri.parse("content://com.havilahshawn.finaltest22ndsesforad.StuProvider/student");
        Cursor cursor = getContentResolver().query(uri, null, null, null, null, null);


        Log.e("TAG",CONTENT_URI.toString());
        if(cursor != null){
            while(cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
                int age = cursor.getInt(cursor.getColumnIndexOrThrow("age"));
                int sex = cursor.getInt(cursor.getColumnIndexOrThrow("sex"));

                
                Student s = new Student();
                s.setName(name);
                s.setAge(age);
                s.setSex(sex);
                students.add(s);
            }

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.add:
                Intent intent = new Intent(MainActivity.this, AddActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }



    @Override
    protected void onStart() {
        super.onStart();


    }
}
